
package com.seekting.study;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class FutureStudy {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Callable<String> callable = new Callable<String>() {

            @Override
            public String call() {
                System.out.println("call start");
                // try {
                // TimeUnit.SECONDS.sleep(2);
                // } catch (InterruptedException e) {
                // // TODO Auto-generated catch block
                // e.printStackTrace();
                // }
                boolean t = true;
                while (t) {

                }
                System.out.println("call end");
                return "call";
            }
        };
        FutureTask<String> futureTask = new FutureTask<String>(callable) {
            @Override
            protected void done() {
                super.done();
                System.out.println("future done!");
            }
        };
        executorService.submit(futureTask);
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        futureTask.cancel(true);
        executorService.shutdown();
        Thread.interrupted();

    }
}
